package com.jlastu.dlxy_androidapp.presenter;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.util.Pair;

import androidx.annotation.NonNull;

import com.jlastu.dlxy_androidapp.base.WeakRefHandler;
import com.jlastu.dlxy_androidapp.contract.ChangePasswordContract;
import com.jlastu.dlxy_androidapp.entity.User;
import com.jlastu.dlxy_androidapp.view.setting.ChangePasswordActivity;

import java.util.Objects;

public class ChangePasswordPresenter implements ChangePasswordContract.IChangePasswordPresenter {
    private static final int WHAT_GET_USER = 0;
    private ChangePasswordContract.IChangePasswordView mChangePasswordView;
    private Context mContext;
    private final String TAG = "ChangePasswordPresenter";
    public ChangePasswordPresenter(ChangePasswordContract.IChangePasswordView changePasswordView, Context context) {
        mChangePasswordView = changePasswordView;
        mContext = context;
    }

    @Override
    public void getUser(int userid, String old_password) {



        final int[] verify = {0};


        new Thread(new Runnable() {
            @SuppressLint("Range")
            @Override
            public void run() {
                //此处异步进行数据查询
                //Uri uri = Uri.parse("content://com.jlastu.znb.provider/goods/1");
                Log.d(TAG, "UserID: " + userid);
                Uri uri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/user/" + userid);
                Cursor cursor = mContext.getContentResolver().query(
                        uri, null, null, null);
                try {
                    if (cursor != null && cursor.moveToFirst()) {
                        do {
                            User user = new User();
                            String user_password = cursor.getString(cursor.getColumnIndex("user_password"));
                            if (Objects.equals(user_password, old_password)) {
                                verify[0] = 1;


                            } else {
                                verify[0] = 2;

                            }
                        } while (cursor.moveToNext());
                    } else {
                        verify[0] = 3;

                    }
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }

                Message message = Message.obtain();
                message.obj = verify[0];
                message.what = WHAT_GET_USER;
                mHandler.sendMessage(message);
            }
        }).start();
    }

    @Override
    public void changePassword(int userid, String new_password) {
        Log.d(TAG, "changePassword: " +new_password);
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 创建用于更新的ContentValues对象
                ContentValues cv = new ContentValues();
                cv.put("user_password", new_password);
                // 构建包含用户名的URI
                Uri userUri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/user/" + userid);
                // 执行更新操作
                int updateCount = mContext.getContentResolver().update(userUri, cv, null, null);
                // 打印更新结果
                if (updateCount > 0) {
                    Log.d("", "更新的用户 ===== " + updateCount);
                } else {
                    Log.d("", "用户不存在");
                }
            }
        }).start();
    }

    Handler mHandler = new WeakRefHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int what = msg.what;
            switch (what) {
                case WHAT_GET_USER:
                    mChangePasswordView.showVerify((int) msg.obj);
                    Log.d(TAG, "判断状态: " + msg.obj);
                    break;
            }
            return false;
        }
    });
}
